export default {
  namespaced: true,
  state: {
    todoList: [],
    currentTodo: null,
    loading: false,
    filters: {
      status: -1, // -1: 全部, 0: 未开始, 1: 进行中, 2: 已完成
      categoryId: -1, // -1: 全部分类
      keyword: ''
    },
    pagination: {
      page: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    SET_TODO_LIST(state, list) {
      state.todoList = list
    },
    SET_CURRENT_TODO(state, todo) {
      state.currentTodo = todo
    },
    SET_LOADING(state, loading) {
      state.loading = loading
    },
    SET_FILTERS(state, filters) {
      state.filters = { ...state.filters, ...filters }
    },
    SET_PAGINATION(state, pagination) {
      state.pagination = { ...state.pagination, ...pagination }
    },
    ADD_TODO(state, todo) {
      state.todoList.unshift(todo)
    },
    UPDATE_TODO(state, todo) {
      const index = state.todoList.findIndex(item => item.id === todo.id)
      if (index !== -1) {
        state.todoList.splice(index, 1, todo)
      }
    },
    DELETE_TODO(state, id) {
      const index = state.todoList.findIndex(item => item.id === id)
      if (index !== -1) {
        state.todoList.splice(index, 1)
      }
    },
    RESET_FILTERS(state) {
      state.filters = {
        status: -1,
        categoryId: -1,
        keyword: ''
      }
      state.pagination = {
        page: 1,
        pageSize: 10,
        total: 0
      }
    }
  },
  actions: {
    // 获取待办列表
    async getTodoList({ commit, state }) {
      commit('SET_LOADING', true)
      try {
        const { page, pageSize } = state.pagination
        const { status, categoryId, keyword } = state.filters
        
        const params = { page, pageSize }
        if (status !== -1) params.status = status
        if (categoryId !== -1) params.categoryId = categoryId
        if (keyword) params.keyword = keyword
        
        const res = await this._vm.$http.get('/api/todo/list', { params })
        if (res.code === 0) {
          commit('SET_TODO_LIST', res.data.list)
          commit('SET_PAGINATION', {
            total: res.data.total
          })
          return Promise.resolve(res.data)
        }
        return Promise.reject(res.msg || '获取待办列表失败')
      } catch (error) {
        return Promise.reject(error)
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 获取待办详情
    async getTodoDetail({ commit }, id) {
      commit('SET_LOADING', true)
      try {
        const res = await this._vm.$http.get(`/api/todo/${id}`)
        if (res.code === 0) {
          commit('SET_CURRENT_TODO', res.data)
          return Promise.resolve(res.data)
        }
        return Promise.reject(res.msg || '获取待办详情失败')
      } catch (error) {
        return Promise.reject(error)
      } finally {
        commit('SET_LOADING', false)
      }
    },
    
    // 创建待办
    async createTodo({ commit }, params) {
      try {
        const res = await this._vm.$http.post('/api/todo', params)
        if (res.code === 0) {
          commit('ADD_TODO', res.data)
          uni.showToast({
            title: '创建成功',
            icon: 'success'
          })
          return Promise.resolve(res.data)
        }
        return Promise.reject(res.msg || '创建待办失败')
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 更新待办
    async updateTodo({ commit }, { id, ...params }) {
      try {
        const res = await this._vm.$http.put(`/api/todo/${id}`, params)
        if (res.code === 0) {
          commit('UPDATE_TODO', res.data)
          uni.showToast({
            title: '更新成功',
            icon: 'success'
          })
          return Promise.resolve(res.data)
        }
        return Promise.reject(res.msg || '更新待办失败')
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 更新待办进度
    async updateTodoProgress({ commit }, { id, progress }) {
      try {
        const res = await this._vm.$http.put(`/api/todo/${id}/progress`, { progress })
        if (res.code === 0) {
          commit('UPDATE_TODO', res.data)
          return Promise.resolve(res.data)
        }
        return Promise.reject(res.msg || '更新进度失败')
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 删除待办
    async deleteTodo({ commit }, id) {
      try {
        const res = await this._vm.$http.delete(`/api/todo/${id}`)
        if (res.code === 0) {
          commit('DELETE_TODO', id)
          uni.showToast({
            title: '删除成功',
            icon: 'success'
          })
          return Promise.resolve(res.data)
        }
        return Promise.reject(res.msg || '删除待办失败')
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 设置筛选条件
    setFilters({ commit, dispatch }, filters) {
      commit('SET_FILTERS', filters)
      commit('SET_PAGINATION', { page: 1 })
      return dispatch('getTodoList')
    },
    
    // 重置筛选条件
    resetFilters({ commit, dispatch }) {
      commit('RESET_FILTERS')
      return dispatch('getTodoList')
    },
    
    // 加载更多
    loadMore({ commit, state, dispatch }) {
      if (state.todoList.length >= state.pagination.total) {
        return Promise.resolve('没有更多数据了')
      }
      
      commit('SET_PAGINATION', { page: state.pagination.page + 1 })
      return dispatch('getTodoList')
    }
  },
  getters: {
    todoList: state => state.todoList,
    currentTodo: state => state.currentTodo,
    loading: state => state.loading,
    filters: state => state.filters,
    pagination: state => state.pagination,
    hasMore: state => state.todoList.length < state.pagination.total
  }
} 